<?php
class Comment extends AppModel {

	var $name = 'Comment';
    var $primaryKey = 'comment_id';
    var $actsAs = array('Containable');
    var $belongsTo = array(
        'Post'=>array(
            'className'=>'Post',
            'counterCache'=>true),        
        'Visitor'=>array(
            'className'=>'Visitor',
            'counterCache'=>true)
    );

  	/**
	 * Validation for comments
	 *
	 * @access public
	 * @var array Validations for model's table's fields
	 */
	var $validate = array(		
		'email'=>array(
			array('rule' => array('between', 5, 30),'message'=>'3'),
			array('rule' => 'email','message'=>'3'),
			array('rule' =>array('minLength',1), 'message' => '3')
            ),
        );
    
    /**
     * Get comments
     * usage in : Post Controller -> function index()
     */
    public function getNewComments(){
         $this->recursive = 0;
         $result = $this->find('all',array(
            'conditions'=>array('Comment.status'=>'1'),
            'fields'=>'Comment.name,Comment.website,Comment.description,Comment.comment_id',
            'contain'=>array(
                'Post'=>array(
                    'fields'=>array('image','slug','title'),
                     'Path'=>array('fields'=>'name')
                    )),
            'limit'=>'4',
            'order'=>'Comment.created DESC'
         ));
     
         return $result;
    }

   /**
    * Fetching all discuss for pagination
    * usage in : Post Controller -> function view()
    */
   function showAllComments(){
      $this->recursive = -1;
      $result = array('fields'=>'Comment.comment_id,Comment.description,Comment.name,Comment.website,Comment.email',
            'order'=>'Comment.created DESC',
            'limit'=>'50',
            );
            
      return $result;
   }

   /**
    * Fetching all discuss for pagination for admin page
    * usage in : Comments Controller -> function manage()
    */
   function getAllComments(){
      $this->recursive = 0;
      $result = array('fields'=>'Comment.comment_id,Comment.description,Comment.name,Comment.website,Comment.email,Comment.visitor_id,Comment.created,Comment.status,Comment.comment_id',
            'contain'=>array(
                'Post'=>array('fields'=>array('title','slug'),'Path'=>array('fields'=>'name'))
            ),
            'order'=>'Comment.created DESC',
            'limit'=>'3',
            );

      return $result;
   }

    /**
     * Validating new comment whether same as before by email, description ,post_id
     * usage in Comment Controller -> functino addComment()
     */
    function checkCommentHistory($comment,$post_id,$email){
       $this->recursive = -1;
       $result = $this->find('count',array(
  			'conditions'=>array('AND'=>array('Comment.description'=>$comment,'Comment.post_id'=>$post_id,'Comment.email'=>$email,"Comment.created >" => date('Y-m-d H:i:s', strtotime("-5 minute")))
       )));

       return $result;
    }

    /**
     * Get counting new incoming comments
     * usage in Comments Controller -> function status()
     */
    function countIncomingComments(){
        $this->recursive = -1;
        $result = $this->find('count',array(
            'conditions'=>array('AND'=>array('Comment.status'=>'0','Comment.read'=>'1'))
        ));
    
        return $result;
    }

    /**
     * get post id by Comment id
     * usage in: Comment Controller -> function delete()
     */
    function getPostIdByComment($comment_id){
        $this->recursive = -1;
        $result = $this->find('first',array(
            'conditions'=>array('Comment.comment_id'=>$comment_id),
           'fields'=>'Comment.post_id' 
        ));

        return $result;
    }

    /**
     * Updating visitor id by email dan visitor id
     * usage in Visitor Controller -> function approve()
     */
    public function updateVisitorId($email,$visitor_id){
        $this->recursive = -1;
        $this->updateAll(array('Comment.visitor_id'=>"'$visitor_id'"),array('Comment.email'=>$email));
    }

    /**
     * Counting comment visitor
     * usage in Visitor Controller -> function approve()
     */
    public function countCommentVisitor($visitor_id){
        $this->recursive = -1;
        $result = $this->find('count',array(
            'conditions'=>array('Comment.visitor_id'=>$visitor_id)
        ));

        return $resut;
    }

    /**
     * Remove visitor id
     * usage in Visitor Controller -> function deny()
     */
    public function removeVisitorId($visitor_id){
        $this->recursive = -1;
        $this->updateAll(array('Comment.visitor_id'=>"0"),array('Comment.visitor_id'=>$visitor_id));
    }

    /**
     * Deleting comments by visitor id
     */
    public function deleteCommentByVisitorId($visitor_id){
        $this->recursive = -1;
        $this->deleteAll(array('Comment.visitor_id'=>$visitor_id));
    }

    /**
     * Get feed comments
     * usage in : Sitemaps Controller -> function comment()
     */
    public function getFeedComments(){
         $this->recursive = 0;
         $result = $this->find('all',array(
            'conditions'=>array('Comment.status'=>'1'),
            'fields'=>'Comment.name,Comment.website,Comment.description,Comment.comment_id',
            'contain'=>array(
                'Post'=>array(
                    'fields'=>array('image','slug','title'),
                     'Path'=>array('fields'=>'name')
                    )),
            'limit'=>'30',
            'order'=>'Comment.created DESC'
         ));

         return $result;
    }
}
?>
